// DOM加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
    // 初始化新闻列表
    initNewsList();
    
    // 初始化顶部导航
    initTopNav();
    
    // 初始化搜索功能
    initSearch();
});

// 初始化新闻列表
function initNewsList() {
    const newsList = document.querySelector('.zheng_news_list');
    const newsItems = newsData.recommend;
    
    // 清空现有内容
    newsList.innerHTML = '';
    
    // 添加新闻项
    newsItems.forEach(news => {
        const newsElement = createNewsElement(news);
        newsList.appendChild(newsElement);
    });
}

// 创建新闻元素
function createNewsElement(news) {
    const article = document.createElement('article');
    article.className = 'zheng_news_item';
    article.innerHTML = `
        <div class="zheng_news_info">
            <h3 class="zheng_news_title">${news.title}</h3>
            <div class="zheng_news_meta">
                <span class="zheng_news_source">${news.source}</span>
                <span class="zheng_news_time">${news.time}</span>
                <span class="zheng_comment_count">${news.commentCount}评论</span>
            </div>
        </div>
        <div class="zheng_news_image">
            <img src="${news.imageUrl}" alt="${news.title}">
        </div>
    `;
    
    // 添加点击事件
    article.addEventListener('click', () => {
        window.location.href = `news_detail.html?id=${news.id}`;
    });
    
    return article;
}

// 初始化顶部导航
function initTopNav() {
    const navItems = document.querySelectorAll('.zheng_scroll_wrapper li');
    
    navItems.forEach(item => {
        item.addEventListener('click', function() {
            // 移除其他项的active类
            navItems.forEach(nav => nav.classList.remove('active'));
            // 添加当前项的active类
            this.classList.add('active');
            
            // 加载对应分类的新闻
            const category = this.textContent.trim();
            loadNewsByCategory(category);
        });
    });
}

// 根据分类加载新闻
function loadNewsByCategory(category) {
    const newsItems = category === '推荐' ? newsData.recommend : 
                     category === '热榜' ? newsData.hot : 
                     newsData.recommend;
    
    const newsList = document.querySelector('.zheng_news_list');
    newsList.innerHTML = '';
    
    newsItems.forEach(news => {
        const newsElement = createNewsElement(news);
        newsList.appendChild(newsElement);
    });
}

// 初始化搜索功能
function initSearch() {
    const searchInput = document.querySelector('.zheng_search input');
    
    searchInput.addEventListener('input', function() {
        const keyword = this.value.trim().toLowerCase();
        if (keyword) {
            searchNews(keyword);
        } else {
            initNewsList();
        }
    });
}

// 搜索新闻
function searchNews(keyword) {
    const allNews = [...newsData.recommend, ...newsData.hot];
    const filteredNews = allNews.filter(news => 
        news.title.toLowerCase().includes(keyword) ||
        news.source.toLowerCase().includes(keyword)
    );
    
    const newsList = document.querySelector('.zheng_news_list');
    newsList.innerHTML = '';
    
    if (filteredNews.length > 0) {
        filteredNews.forEach(news => {
            const newsElement = createNewsElement(news);
            newsList.appendChild(newsElement);
        });
    } else {
        newsList.innerHTML = '<div class="zheng_empty_result">没有找到相关内容</div>';
    }
} 